$(document).ready(function () {
	function Goods (name, id, price, discount, isPreferential, unit) {
		this.name = name;
		this.id = id;
		this.unitPrice = price;
		this.num = 0;
		this.discount = discount;
		this.isPreferential = isPreferential;
		this.unit = unit;
	}

	Goods.prototype = {
		getTotal : function () {
			if (this.isPreferential === false) {
				return this.unitPrice*this.num*this.discount;	
			} else {
				return (parseInt(this.num/3)*2+this.num%3)*this.unitPrice*this.discount;
			}
			
		},
		getTotalSave : function () {
			return this.unitPrice*this.num - this.getTotal();
		},
		getGiving : function () {
			if (this.isPreferential === true) {
				return parseInt(this.num/3);
			}
			return 0;
		}
	}

	

	$(document).on("blur", ".js-goods-data", function () {
		$(".content-show div").remove();
		var tomato = new Goods("土豆", "ITEM000004", 1.50, 1, false, "斤");
		var hamburger = new Goods("汉堡", "ITEM000005", 8.00, 1, false, "个");
		var coco = new Goods("可口可乐", "ITEM000001", 3.00, 1, false, "瓶");
		var badminton = new Goods("羽毛球", "ITEM000002", 1.00, 1, true, "个");
		var apple = new Goods("苹果", "ITEM000003", 3.00, 0.95, true, "斤");

		var arrBase = new Array();
		arrBase.push(coco);
		arrBase.push(badminton);
		arrBase.push(apple);
		arrBase.push(tomato);
		arrBase.push(hamburger);


		var $this = $(this);
		var arr = new Object();
		var inpuData = eval("("+$this.val()+")");
		var showData = {
			base : "",
			preferential : "",
			total : 0,
			saveTotal : 0

		};
		if (undefined === inpuData || "object" !== typeof inpuData || 0 === inpuData.length) {
			return false;
		}
		for (var i = 0; i < inpuData.length; i++) {
			var num = 1;
			var str = inpuData[i];
			if (inpuData[i].indexOf('-') > -1) {
				num = parseInt(inpuData[i].split('-')[1]);
				str = inpuData[i].split('-')[0];
			}	
			
			for (var o = 0; o < arrBase.length; o++) {
				if (str === arrBase[o].id) {
					arrBase[o].num += num;
				}
			}
		}

		for (var o = 0; o < arrBase.length; o++) {
			if (0 === arrBase[o].num) {
				continue;
			}
			var goodsbase = "<span>名称 : "+arrBase[o].name+"</span>"
							+" , <span>数量 : "+arrBase[o].num+arrBase[o].unit+"</span>"
							+" , <span>单价 : "+arrBase[o].unitPrice+"(元)</span>"
							+" , <span>小计 : "+arrBase[o].getTotal().toFixed(2)+"(元)</span>";
			if (1 !== arrBase[o].discount) {
				goodsbase += " , <span>节省 : "+arrBase[o].getTotalSave().toFixed(2)+"(元)</span>";
			}
			goodsbase = "<div>"+goodsbase+"</div>";
			showData.base += goodsbase;	
			
			
			if (0 !== arrBase[o].getGiving()) {
				var goodsPreferential = "<div><span>名称 : "+arrBase[o].name+"</span>"
										+" , <span>名称 : "+arrBase[o].getGiving()+arrBase[o].unit+"(元)</span></div>";
				showData.preferential += goodsPreferential;
			}

			showData.total += arrBase[o].getTotal();
			showData.saveTotal += arrBase[o].getTotalSave();
		}

		if (0 !== showData.saveTotal) {
			if (showData.preferential) {
				$("<div class='shop-header'>***<没钱赚商店>购物清单***</div>"
				+"<div class='shop-list'>"+showData.base+"</div>"
				+"<div class='shop-preferential'>买二赠一商品：</div>"
				+"<div class='shop-list'>"+showData.preferential+"</div>"
				+"<div class='shop-total'> 总计 : "+showData.total.toFixed(2)+"(元)</div>"
				+"<div> 节省 : "+showData.saveTotal.toFixed(2)+"(元)</div>").appendTo(".content-show");
			} else {
				$("<div class='shop-header'>***<没钱赚商店>购物清单***</div>"
				+"<div class='shop-list'>"+showData.base+"</div>"
				+"<div class='shop-total'> 总计 : "+showData.total.toFixed(2)+"(元)</div>"
				+"<div> 节省 : "+showData.saveTotal.toFixed(2)+"(元)</div>").appendTo(".content-show");
			}
			
		} else {
			$("<div class='shop-header'>***<没钱赚商店>购物清单***</div>"
				+"<div class='shop-list'>"+showData.base+"</div>"
				+"<div class='shop-total'> 总计 :"+showData.total.toFixed(2)+"(元)</div>").appendTo(".content-show");
		}
 	});
});
